/*
 * @Author: szx
 * @Date: 2021-12-24 14:33:20
 * @LastEditTime: 2021-12-24 14:50:57
 * @Description:
 * @FilePath: \leetcode\600-699\617\617.js
 */

function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function (root1, root2) {
    if (!root1) return root2;
    if (!root2) return root1;
    root1.val += root2.val;
    root1.left = mergeTrees(root1.left, root2.left);
    root1.right = mergeTrees(root1.right, root2.right);
    return root1;
};

var mergeTrees = function (root1, root2) {
    if (!root1) return root2;
    if (!root2) return root1;
    const que = [];
    que.push(root1);
    que.push(root2);
    while (que.length) {
        const t1 = que.shift();
        const t2 = que.shift();
        t1.val += t2.val;
        if (t1.left && t2.left) {
            que.push(t1.left);
            que.push(t2.left);
        }
        if (t1.right && t2.right) {
            que.push(t1.right);
            que.push(t2.right);
        }
        if (!t1.left && t2.left) {
            t1.left = t2.left;
        }
        if (!t1.right && t2.right) {
            t1.right = t2.right;
        }
    }
    return root1;
};
